package cn.edu.sgu.www.mhxysy.service.qiling.impl;

import cn.edu.sgu.www.mhxysy.vo.Imager;
import cn.edu.sgu.www.mhxysy.entity.equipment.EquipmentCategory;
import cn.edu.sgu.www.mhxysy.entity.qiling.QilingCategory;
import cn.edu.sgu.www.mhxysy.entity.qiling.QilingType;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentTypeMapper;
import cn.edu.sgu.www.mhxysy.mapper.qiling.QilingCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.qiling.QilingTypeMapper;
import cn.edu.sgu.www.mhxysy.service.qiling.QilingTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class QilingTypeServiceImpl implements QilingTypeService {

    private final QilingTypeMapper qilingTypeMapper;
    private final EquipmentTypeMapper equipmentTypeMapper;
    private final QilingCategoryMapper qilingCategoryMapper;
    private final EquipmentCategoryMapper equipmentCategoryMapper;

    @Autowired
    public QilingTypeServiceImpl(
            QilingTypeMapper qilingTypeMapper,
            EquipmentTypeMapper equipmentTypeMapper,
            QilingCategoryMapper qilingCategoryMapper,
            EquipmentCategoryMapper equipmentCategoryMapper) {
        this.qilingTypeMapper = qilingTypeMapper;
        this.equipmentTypeMapper = equipmentTypeMapper;
        this.qilingCategoryMapper = qilingCategoryMapper;
        this.equipmentCategoryMapper = equipmentCategoryMapper;
    }

    @Override
    public void initData() {
        List<Imager> list = equipmentTypeMapper.selectAll();

        for (Imager imager : list) {
            Integer equipmentId = Integer.parseInt(imager.getId());

            QilingCategory qilingCategory = qilingCategoryMapper.selectDetail(equipmentId);

            QilingType qilingType = new QilingType();

            qilingType.setId(null);
            qilingType.setName(imager.getName() + " · 器灵");
            qilingType.setImage(qilingCategory.getImage());
            qilingType.setEquipmentId(equipmentId);

            qilingTypeMapper.insert(qilingType);
        }
    }

    @Override
    public void updateTypes() {
        List<QilingCategory> list = qilingCategoryMapper.selectList(null);

        for (QilingCategory qilingCategory : list) {
            // 查询装备类型
            Integer equipmentId = qilingCategory.getEquipmentId();
            EquipmentCategory equipmentCategory = equipmentCategoryMapper.selectById(equipmentId);

            // 通过装备类型ID查询器灵类型
            QueryWrapper<QilingType> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("equipment_id", equipmentCategory.getType());

            QilingType qilingType = qilingTypeMapper.selectOne(queryWrapper);

            // 修改器灵类型信息
            qilingCategory.setType(qilingType.getId());

            qilingCategoryMapper.updateById(qilingCategory);
        }
    }

    @Override
    public List<QilingType> selectAll() {
        return qilingTypeMapper.selectList(null);
    }

    @Override
    public QilingType selectById(Integer id) {
        return qilingTypeMapper.selectById(id);
    }

}